//
//  oldMyParentsViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/26.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMyParentsViewController.h"
#import "oldPersonIntroduceTableViewCell.h"
#import "oldMyParentsHeaderView.h"
#import "oldFamilyParentListTableViewController.h"
#import "oldFamilyEditHealthViewController.h"
#import "oldFamilyEditRelationViewController.h"

@interface oldMyParentsViewController ()

@end

static NSString *const identifier = @"oldPersonIntroduceTableViewCell";

@implementation oldMyParentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"家属资料";
    [self rigisterCell:identifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self initHeader];
    [self setRightBtn:@"保存"];
}

- (void)initHeader
{
    oldMyParentsHeaderView *view = [[[NSBundle mainBundle] loadNibNamed:@"oldMyParentsHeaderView"
                                                                  owner:self
                                                                options:nil] objectAtIndex:0];
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    self.tableView.tableHeaderView = header;
    view.frame = header.bounds;
    [header addSubview:view];
    view.url = _parent.avatar;
}

- (void)setText:(NSString *)text
{
    _text = text;
    [self.tableView reloadData];
}

#pragma mark - initData
- (void)initData
{
    _text = _parent.userRelation.desc;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 5;
            break;
        default:
            return 1;
            break;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            return [self configFristSection:tableView indexPath:indexPath];
            break;
        default:
            return [self configLastSection:tableView indexPath:indexPath];;
            break;
    }
}


- (UITableViewCell *)configFristSection:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = OLD_BLACK;
    cell.detailTextLabel.textColor = OLD_BLACK;
    
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = @"姓名";
            cell.detailTextLabel.text = _parent.uname;
            break;
        case 1:
            cell.textLabel.text = @"性别";
            cell.detailTextLabel.text = [_parent getAccountSex];
            break;
        case 2:
            cell.textLabel.text = @"年龄";
            cell.detailTextLabel.text = [_parent getNStringOfAge];
            break;
        case 3:
            cell.textLabel.text = @"手机号码";
            cell.detailTextLabel.text = _parent.mobile;
            break;
        case 4:
            cell.textLabel.text = @"亲属关系";
            cell.detailTextLabel.text = _parent.userRelation.relation;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 5:
            break;
        default:
            break;
    }
    
    return cell;
    
}

- (UITableViewCell *)configLastSection:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    oldPersonIntroduceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldPersonIntroduceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    if (_parent.userRelation.desc) {
        cell.label_text.text = _parent.userRelation.desc;
    }
    else
    {
        cell.label_text.text = @"点击编辑备注信息";
    }
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }
    
    return 146.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 4) {
            oldFamilyEditRelationViewController *vc = [oldFamilyEditRelationViewController new];
            vc.parent = _parent;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else if (indexPath.section == 1) {
        oldFamilyEditHealthViewController *vc = [oldFamilyEditHealthViewController new];
        vc.parent = _parent;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
